/**
 * 
 */
package com.antilia.infomaresme.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackDefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.odlabs.wiquery.core.IWiQueryPlugin;
import org.odlabs.wiquery.core.javascript.JsStatement;
import org.odlabs.wiquery.ui.commons.WiQueryUIPlugin;
import org.odlabs.wiquery.ui.tabs.Tabs;

import com.antilia.infomaresme.InfomaresmeApplication;
import com.antilia.infomaresme.InfomaresmeSession;
import com.antilia.infomaresme.data.dataobjects.Category;
import com.antilia.infomaresme.data.dataobjects.Item;
import com.antilia.infomaresme.data.dataobjects.UserItemAlert;
import com.antilia.infomaresme.data.dataobjects.UserMessage;
import com.antilia.infomaresme.data.dataobjects.VisibilityStatus;
import com.antilia.infomaresme.pages.alert.edit.EditItemUserAlertPanel;
import com.antilia.infomaresme.pages.details.DetailsPanel;
import com.antilia.infomaresme.pages.details.DetailsViewLinkPanel;
import com.antilia.infomaresme.pages.details.message.UserMessagePanel;
import com.antilia.infomaresme.pages.signin.LogInContextPanel;
import com.antilia.infomaresme.provider.ItemsDataProvider;
import com.antilia.infomaresme.resources.InfoModalWindow;
import com.wiquery.plugin.raty.RatingPanel;

/**
 * @author Ernesto
 *
 */
@WiQueryUIPlugin
public class ChooseCategoryPanel extends Panel implements IWiQueryPlugin {

	private static final long serialVersionUID = 1L;
	
	private Category selected;
	
	private Category place;
	
	private ItemsDataProvider  dataProvider;
	
	private AjaxFallbackDefaultDataTable<Item> items;
	
	private ModalWindow modal;
	
	private Tabs tabs;
	
	private LogInContextPanel logInContextPanel;
	
	/**
	 * @param id
	 */
	public ChooseCategoryPanel(String id) {
		super(id);
		setOutputMarkupId(true);		
		modal = new InfoModalWindow("modal");
		modal.setTitle("Detalles");
		modal.setInitialHeight(640);
		modal.setInitialWidth(950);
		modal.setResizable(false);
		add(modal);
	  	tabs = new Tabs("tabs");
	  	add(tabs);
	}
	
	@Override
	protected void onBeforeRender() {
		RepeatingView choosenCategory = new RepeatingView("choosenCategory");
		tabs.addOrReplace(choosenCategory);
		
		WebMarkupContainer wm  = new WebMarkupContainer(choosenCategory.newChildId());
			choosenCategory.add(wm);
			wm.add(new TopLevelLinkPanel("category",new ResourceModel("all")) {
				
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					selected = null;
					target.add(ChooseCategoryPanel.this);	
				}
			}
		);
			
		if(selected != null) {
			List<Category> parents = selected.getParentChain();
			for(Category category: parents) {
				wm  = new WebMarkupContainer(choosenCategory.newChildId());
				choosenCategory.add(wm);
				wm.add(new CategoryLinkPanel("category", category) {
					
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						selected = getCategory();
						target.add(ChooseCategoryPanel.this);
					}
				});
			}
			
			wm  = new WebMarkupContainer(choosenCategory.newChildId());
			choosenCategory.add(wm);
			wm.add(new CategoryLinkPanel("category", selected) {
					
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						selected = getCategory();
						target.add(ChooseCategoryPanel.this);
					}
				});
		} 
		
		
		RepeatingView choosenPlace = new RepeatingView("choosenPlace");
		tabs.addOrReplace(choosenPlace);
		
		wm  = new WebMarkupContainer(choosenPlace.newChildId());
			choosenPlace.add(wm);
			wm.add(new TopLevelLinkPanel("category",new ResourceModel("allM")) {
				
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					place = null;
					target.add(ChooseCategoryPanel.this);	
				}
			}
		);
			
		if(place != null) {
			List<Category> parents = place.getParentChain();
			for(Category category: parents) {
				wm  = new WebMarkupContainer(choosenPlace.newChildId());
				choosenPlace.add(wm);
				wm.add(new CategoryLinkPanel("category", category) {
					
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						place = getCategory();
						tabs.setDefaultSelectedTabIndex(1);
						target.add(ChooseCategoryPanel.this);
					}
				});
			}
			
			wm  = new WebMarkupContainer(choosenPlace.newChildId());
			choosenPlace.add(wm);
			wm.add(new CategoryLinkPanel("category", place) {
					
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						place = getCategory();
						tabs.setDefaultSelectedTabIndex(0);
						target.add(ChooseCategoryPanel.this);
					}
				});
		} 
		
		
		CategoriesListPanel categorias = new CategoriesListPanel("categorias", selected, new ResourceModel("chooseFilter")) {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				selected = getSelected();
				tabs.setDefaultSelectedTabIndex(0);
				target.add(ChooseCategoryPanel.this);
			}
			
			@Override
			protected List<Category> findTopLevel() {
				return InfomaresmeApplication.getQAApplication().getCategoriesDao().findTopLevel();
			}
		};
		
		tabs.addOrReplace(categorias);
				
		CategoriesListPanel places = new CategoriesListPanel("places", place, new ResourceModel("filterByPlace")) {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				place = getSelected();
				tabs.setDefaultSelectedTabIndex(1);
				target.add(ChooseCategoryPanel.this);
			}
			
			@Override
			protected List<Category> findTopLevel() {
				return InfomaresmeApplication.getQAApplication().getCategoriesDao().findTopLevelGeographic();
			}
		};
		
		tabs.addOrReplace(places);
		
		List<IColumn<Item>> columns = new ArrayList<IColumn<Item>>();	
		columns.add(new PropertyColumn<Item>(Model.of(""), null , "price") {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void populateItem(
					org.apache.wicket.markup.repeater.Item<ICellPopulator<Item>> item,
					String componentId, final IModel<Item> rowModel) {
				item.add(new DetailsViewLinkPanel(componentId, rowModel) {
					
					private static final long serialVersionUID = 1L;

					

					@Override
					public void onSee(AjaxRequestTarget target,
							IModel<Item> model) {
						modal.setTitle(new ResourceModel("itemDetails"));
						modal.setInitialHeight(640);
						modal.setInitialWidth(950);
						modal.setContent(new DetailsPanel(modal.getContentId(),model));
						modal.show(target);
					}
					
					public void onSendMessage(AjaxRequestTarget target, org.apache.wicket.model.IModel<Item> model) {
						modal.setTitle(new ResourceModel("message.Send"));
						modal.setInitialHeight(440);
						modal.setInitialWidth(650);
						modal.setContent(new UserMessagePanel(modal.getContentId(), model){
							
							private static final long serialVersionUID = 1L;

							@Override
							protected void onSend(AjaxRequestTarget target,
									UserMessage message) {
								modal.close(target);
								
							}
						});
						modal.show(target);
					};
					
					@Override
					public void onAlert(AjaxRequestTarget target, final IModel<Item> model, final IModel<UserItemAlert> alertModel) {
						if(InfomaresmeSession.getSession().isAuthenticated()) {
							modal.setTitle(new ResourceModel("actions.createNewAlerta"));
							modal.setInitialHeight(400);
							modal.setInitialWidth(600);
				
							modal.setContent(new EditItemUserAlertPanel(modal.getContentId(),model, alertModel.getObject()) {
								
								private static final long serialVersionUID = 1L;

								@Override
								public void onSaved(AjaxRequestTarget target,
										Form<?> form) {
									modal.close(target);
									target.add(items);
								}
							});
							modal.show(target);
						} else {
							logInContextPanel = new LogInContextPanel(modal.getContentId()) {
								
								private static final long serialVersionUID = 1L;
								
								@Override
								public void onLogIn(AjaxRequestTarget target) {
									// reload alert now that we have the user.
									UserItemAlert alert = InfomaresmeApplication.getQAApplication().getAlertsDao().getAlert(InfomaresmeSession.getSession().getUser(), model.getObject());
									ChooseCategoryPanel.this.logInContextPanel.setContents(new EditItemUserAlertPanel(
											LogInContextPanel.CONTENT_ID,model, alert) {
										
										private static final long serialVersionUID = 1L;

										@Override
										public void onSaved(
												AjaxRequestTarget target,
												Form<?> form) {
											modal.close(target);
											target.add(items);
										}
									});
									target.add(ChooseCategoryPanel.this.logInContextPanel);
								}
							};
							modal.setInitialHeight(400);
							modal.setInitialWidth(600);
							modal.setTitle(new ResourceModel("actions.createNewAlerta"));
							modal.setContent(logInContextPanel);
							modal.show(target);
						}
					}
				});
			}
		});
		columns.add(new PropertyColumn<Item>(new ResourceModel("description"), null, "title"));
		columns.add(new PropertyColumn<Item>(new ResourceModel("location"), null, "place"));
		columns.add(new PropertyColumn<Item>(new ResourceModel("price"), "price" , "price"));
		columns.add(new PropertyColumn<Item>(new ResourceModel("rating"), "rating" , "rating") {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void populateItem(
					org.apache.wicket.markup.repeater.Item<ICellPopulator<Item>> item,
					String componentId, IModel<Item> rowModel) {
				item.add(new RatingPanel(componentId, 5, rowModel.getObject().getRating()).setReadOnly(true));
				
			}
		});
		
		
		
 		dataProvider = new ItemsDataProvider() {
 		 	
 		 	private static final long serialVersionUID = 1L;

			@Override
 		 	public Category getCategory() {
 		 		return selected;
 		 	}
 		 	
 		 	@Override
 		 	public Category getPlace() {
 		 		return place;
 		 	}
 		 	
 		 	public com.antilia.infomaresme.data.dataobjects.User getUser() {
 		 		return null;
 		 	};
 		 	
 		 	@Override
 		 	protected VisibilityStatus getItemStatus() {
 		 		// display only visible items.
 		 		return VisibilityStatus.visible;
 		 	}
 		};		
		items  = new AjaxFallbackDefaultDataTable<Item>("items", columns, dataProvider, 10);
		addOrReplace(items);
		super.onBeforeRender();
	}


	public Category getSelected() {
		return selected;
	}

	public void setSelected(Category selected) {
		this.selected = selected;
	}

	public JsStatement statement() {
		return null;
	}

}
